package com.longge.wechat.controller.wx;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.longge.wechat.communication.Poster;
import com.longge.wechat.message.resp.RespTextMessage;
import com.longge.wechat.message.post.PostVoiceMessage;
import com.longge.wechat.message.post.Voice;
import com.longge.wechat.message.wxUtil.WeiXinUtil;
import com.longge.wechat.utils.XmlUtil;



@Controller
public class PostVoiceController {


	@RequestMapping(value = "/showVoice.do", method = RequestMethod.POST)
	protected void doPost1(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String content = "";
		String openId = (String) request.getAttribute("openId");
		
		content="对不起你没有语音消息";
		
		RespTextMessage message = new RespTextMessage(
				openId,
				(String) request.getAttribute("from"),content);
		Poster.post(response, XmlUtil.getInstance().bean2xml(message,null,null));
	}
	
	@RequestMapping(value = "/respVoice.do", method = RequestMethod.POST)
	protected void doPost2(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String content = "";
		String openId = (String) request.getAttribute("openId");
		PostVoiceMessage vm = new PostVoiceMessage();
		vm.setTouser(openId);
		vm.setMsgtype("voice");
		Voice voice = new Voice();
		
		voice.setMedia_id("media_id");
		vm.setVoice(voice);
		int result = WeiXinUtil.sendInfo(vm);
		if(result == 1) {
			content = "发送语音消息成功";
		}
		System.out.println(content);
	}
}
